/**
 * Created by Administrator on 2017/5/18.
 */

import {USER_INFO, LOADING, SET_TIP_INO, JELLY_FROM, TOKEN_SET,CHECK_TIMEOUT}   from './mutation-type'
import  {
  LOADING_START,
  LOADING_ENDING,
  LOADING_TIMEOUT,
  LOADING_REST,
  LOADING_REQUEST_TIMEOUT
} from '../static/comm/TipMessage';
import {checkLoginTimeout} from '../static/comm/commom'
const mutations = {
  [USER_INFO](state, {info}){
    state.userInfo = {...info};
  },
  [LOADING](state, {code, meta, custom_info}){
    let information = '';
    switch (code) {
      case 100:
        information = LOADING_START;
        break;
      case 101:
        information = LOADING_ENDING;
        break;
      case 102:
        information = LOADING_TIMEOUT;
        break;
      case 120:
        information = custom_info;
        break;
      default:
        information = LOADING_REST;
    }
    state.tipInfo = information;
    if (code === 102 || code === 101 || code === 120) {
      let timeout_index = setTimeout(() => {
        state.loading = meta;
        clearTimeout(timeout_index);
        state.tipInfo = LOADING_REST;
      }, LOADING_REQUEST_TIMEOUT);
    } else {
      state.loading = meta;
    }
  },
  [SET_TIP_INO](state, info){
    state.tipInfo = info;
  },
  [JELLY_FROM](state, config){
    state.jelly_from = config;
  },[TOKEN_SET](state,{survive}){
    state.tokenAuth=survive;
  },
  [CHECK_TIMEOUT](state){
    let expire    =state.userInfo.expire;
    let result  =checkLoginTimeout(expire);
    if(false===result){state.userInfo='';}
  }
}


export default mutations
